<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\model;

use app\common\model\store\Collect as CollectModel;
use think\model\relation\HasOne;

/**
 * 用户收藏表模型
 * Class Store
 * @package app\store\model
 */
class Collect extends CollectModel
{
    /**
     * 隐藏的字段
     * @var string[]
     */
    protected $hidden = [
        'create_time'
    ];

    /**
     * 关联logo图片
     * @return HasOne
     */
    // public function logoImage(): HasOne
    // {
    //     return $this->hasOne('UploadFile', 'file_id', 'image_id')
    //         ->bind(['image_url' => 'preview_url']);
    // }

    /**
     * 获取当前商城的基本信息
     * @return Store|array|null
     */
    public static function getInfo()
    {
        return static::detail(static::$storeId);
    }

      /**
     * 获取当前商城的基本信息
     * @return Store|array|null
     */
    public  function getCollectInfo($userId,$goodsId)
    {
        $list = $this->where('user_id', '=', $userId)->where('goods_id', '=', $goodsId)
            ->where('store_id', '=', static::$storeId)
            ->select();
        return $list;
    }

    //新增收藏记录
    public function add($userId,$goodsId){
         // 判断是否已存在购物车记录
         $detail =  $this->getCollectInfo($userId,$goodsId);
         if(!$detail->toArray()){
            return $this->save([
                'goods_id' => $goodsId,
                'user_id' => $userId,
                'store_id' => self::$storeId,
                'create_time'=>time()
            ]);
         }else{
            return false;
         }    
    }

    //取消收藏记录
    public function clear($userId,$goodsId) {
        // 判断是否已存在购物车记录
        $detail =  $this->getCollectInfo($userId,$goodsId);
        if($detail->toArray()){
            static::deleteAll([
                ['goods_id', '=', $goodsId],
                ['user_id', '=', $userId]
            ]);
            return true;
        }else{
           return false;
        }    
   }


    /**
     * 获取列表
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    // public function getCollectList($user_id){
        
    //     $list = $this->where('user_id', '=', $user_id)
    //         ->where('store_id', '=', static::$storeId)
    //         ->paginate(15);
    //     return $list;
    // }

    /**
     * 获取我的收藏明细列表
     * @param array $param
     * @return \think\Paginator
     */
    public function getCollectList($param = [])
    {
        // 设置查询条件
        $filter = $this->getFilter($param);
        // 获取列表数据
        $list = $this
            ->alias('c')
            ->field('c.collect_id,c.create_time as collect_time,goods.*,user.user_id,user.mobile,user.nick_name,user.avatar_id')
            ->where($filter)
            // ->where('c.store_id', '=', static::$storeId)
            ->leftJoin('goods', 'goods.goods_id = c.goods_id')
            ->leftJoin('user', 'user.user_id = c.user_id')
            ->order(['c.create_time' => 'desc', $this->getPk()])
            ->paginate(10);
            // var_dump($this->getLastSql()) ;
       
        return $list;
    }


     /**
     * 设置查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param): array
    {
        // 设置默认的检索数据
        $params = $this->setQueryDefaultValue($param, [
            'userId' => 0,          // 会员ID
            'search' => '',         // 搜索商品名称关键字
            // 'betweenTime' => [],    // 起止时间
        ]);
        // 检索查询条件
        $filter = [];
        // 用户ID
        $params['userId'] > 0 && $filter[] = ['c.user_id', '=', $params['userId']];
        // 搜索内容: 用户昵称
        !empty($params['search']) && $filter[] = ['goods.goods_name', 'like', "%{$params['search']}%"];
        // 起止时间
        // if (!empty($params['betweenTime'])) {
        //     $times = between_time($params['betweenTime']);
        //     $filter[] = ['c.create_time', '>=', $times['start_time']];
        //     $filter[] = ['c.create_time', '<', $times['end_time'] + 86400];
        // }
        return $filter;
    }

}
